<template>
  <div class="app-container">
    <el-card>
      <el-form :model="queryList" :inline="true" label-width="68px">
        <el-form-item label="物料单位">
          <el-input v-model="queryList.name" placeholder="请输入物料单位" clearable style="width: 100%" />
        </el-form-item>
        <el-form-item label="物料编号">
          <el-input v-model="queryList.code" placeholder="请输入物料编号" clearable style="width: 100%" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-plus" v-hasPermi="['basic:nccBasic:add']" @click="addDialog('add')">新增</el-button>
          <el-button type="primary" icon="el-icon-search" v-hasPermi="['basic:nccBasic:query']" @click="filterDire">查询</el-button>
          <el-button type="danger" icon="el-icon-refresh" @click="clearAll">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-table :data="materialList" border height="605px">
        <el-table-column type="index" label="序号" align="center"> </el-table-column>
        <el-table-column prop="code" label="物料代码" align="center"></el-table-column>
        <el-table-column prop="name" label="物料单位" align="center"></el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
          <template slot-scope="scope">
            <el-button  v-hasPermi="['basic:nccBasic:edit']" size="mini" type="text" icon="el-icon-edit" @click="addDialog('edit',scope.row)">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete"
                       @click="deleteBtn(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination background layout=" total,prev, pager, next, sizes,jumper" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" :current-page="queryList.pageNum" :page-size="queryList.pageSize"
        :page-sizes="[10, 20, 50,100]" :total="total">
      </el-pagination>
    </el-card>
    <materialAdd ref="materialAddRef" :visible.sync="visible" @addSuccess="addSuccess"/>
  </div>
</template>

<script>
  import materialAdd from "@/components/BasicInformation/materialAdd.vue"
  import {
    unitDelete,
    findNccBasicUnitData,
  } from '@/api/basic/PersonnelInformation.js'
  export default {
    components: {
      materialAdd
    },
    data() {
      return {
        queryList: {
          name: '', //物料名称
          code: '', //物料编号
        },
        materialList: [], //el-table数据
        total: 0,
        visible: false,
      };
    },
    created() {
      this.getNccMaterialList();
    },
    methods: {
      //删除
      deleteBtn(row) {
        this.$confirm(`确认要进行删除吗？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          unitDelete({id:row.id}).then(res => {
            this.$message.success(res.msg);
            this.getNccMaterialList();
          })
        }).catch(()=>{
          this.$message.info('已取消');
        })
      },
      getNccMaterialList() {
        findNccBasicUnitData(this.queryList).then(response => {
          this.materialList = response.rows;
          this.total = response.total;
        });
      },
      //新增/修改后刷新列表
      addSuccess(){
        this.getNccMaterialList();
      },
      clearAll() {
        this.queryList = {
          name: "",
          code: null,
        };
        this.getNccMaterialList();
      },
      filterDire() {
        this.getNccMaterialList()
      },

      addDialog(type, row) {
        this.visible = true;
        this.$refs.materialAddRef.openDialog(type, row);
      },
      handleSizeChange(newsize) {
        this.queryList.pageNum = 1
        this.queryList.pageSize = newsize
        this.getNccMaterialList()
      },
      handleCurrentChange(newpage) {
        this.queryList.pageNum = newpage
        this.getNccMaterialList()
      },
    }
  };
</script>

<style scoped>
  .app-container {
    padding: 20px;
  }

  .el-card {
    margin-bottom: 20px;
  }

  .btn el-button {
    margin-bottom: 20px;
  }

  .el-pagination {
    float: right;
    margin: 20px;
  }
</style>
